import { legacy_createStore as createStore } from 'redux';

const initState = {
  count: 0,
  msg: 'hello',
  title: 'hello React',
  list: [],
  detail: {},
};

function xxxReducer(state = initState, { type, payload }) {
  switch (type) {
    case 'add':
      // 2. payload接收参数
      return { ...state, count: state.count + payload };
    case 'des':
      return { ...state, count: state.count - payload };
    default:
      return state;
  }
}

const store = createStore(xxxReducer);

export default store;

// 1. 封装函数，函数调用返回一个Action对象
export const addAction = (num) => {
  return {
    type: 'add',
    payload: num,
  };
};

export const desAction = (num) => {
  return {
    type: 'des',
    payload: num,
  };
};
